package com.alibaba.cloud.client9092.service.fallback;

import com.alibaba.cloud.client9092.service.ClientFeignService;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * created by wuquan at 05/09/2021
 * ^_^ ^_^
 */
@Slf4j
@Component
public class ClientFeignServiceFallbackFactory implements FallbackFactory<ClientFeignService> {
    @Override
    public ClientFeignService create(Throwable throwable) {
        return new ClientFeignService() {
            @Override
            public String get() {
                if (throwable instanceof FlowException) {
                    return "流控";
                }
                if (throwable instanceof DegradeException) {
                    return "降级";
                }

                log.error("error",throwable);
                return "异常";

            }

            @Override
            public String save(String name) {
                return null;
            }
        };
    }
}
